package org.tankwar.controller;

import org.tankwar.type.GameType;
import org.tankwar.view.GamePanel;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * 坦克控制器，响应用户输入不同的键位对坦克的控制
 */
public class TankController extends KeyAdapter { // 继承键盘事件监听适配器
    private final GamePanel gamePanel; // 组合游戏面板，用于显示模型到视图

    public TankController(GamePanel gamePanel) {
        this.gamePanel = gamePanel;
    }

    /**
     * 键盘按压事件监听
     * @param e 事件源
     */
    @Override
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        if (gamePanel.homeTanks.isEmpty()) return;
        // 监听键盘，控制坦克移动
        switch (key) {
            case KeyEvent.VK_R: // 当按下R时，重新开始游戏
                gamePanel.printable = true;
                gamePanel.getMainFrame().setGamePanel(new GamePanel(gamePanel.getMainFrame()));
                break;
            case KeyEvent.VK_D: // 监听向右键 玩家坦克1
                gamePanel.homeTanks.get(0).setToR(true);
                break;
            case KeyEvent.VK_A:// 监听向左键 玩家坦克1
                gamePanel.homeTanks.get(0).setToL(true);
                break;
            case KeyEvent.VK_W: // 监听向上键 玩家坦克1
                gamePanel.homeTanks.get(0).setToU(true);
                break;
            case KeyEvent.VK_S:// 监听向下键 玩家坦克1
                gamePanel.homeTanks.get(0).setToD(true);
                break;
            case KeyEvent.VK_UP:// 监听向上键 玩家坦克2
                if (gamePanel.gameType == GameType.TWO_PLAYER_MODE) // 当玩家2存在的时候才设置
                    gamePanel.homeTanks.get(1).setToU(true);
                break;
            case KeyEvent.VK_LEFT:// 监听向左键 玩家坦克2
                if (gamePanel.gameType == GameType.TWO_PLAYER_MODE)
                    gamePanel.homeTanks.get(1).setToL(true);
                break;
            case KeyEvent.VK_DOWN:// 监听向下键 玩家坦克2
                if (gamePanel.gameType == GameType.TWO_PLAYER_MODE)
                    gamePanel.homeTanks.get(1).setToD(true);
                break;
            case KeyEvent.VK_RIGHT:// 监听向右键 玩家坦克2
                if (gamePanel.gameType == GameType.TWO_PLAYER_MODE)
                    gamePanel.homeTanks.get(1).setToR(true);
                break;
        }
    }

    /**
     * 键盘释放事件监听
     * @param e 事件源
     */
    @Override
    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        if (gamePanel.homeTanks.isEmpty()) return;
        switch (key) {
            case KeyEvent.VK_F: // 坦克1开火监听
                gamePanel.homeTanks.get(0).fire();
                break;
            case KeyEvent.VK_NUMPAD7: // 坦克1开火监听
                if (gamePanel.gameType == GameType.TWO_PLAYER_MODE)
                    gamePanel.homeTanks.get(1).fire();
                break;
            case KeyEvent.VK_D: // 坦克1停止向右移动
                gamePanel.homeTanks.get(0).setToR(false);
                break;

            case KeyEvent.VK_A: // 坦克1停止向左移动
                gamePanel.homeTanks.get(0).setToL(false);
                break;

            case KeyEvent.VK_W: // 坦克1停止向上移动
                gamePanel.homeTanks.get(0).setToU(false);
                break;

            case KeyEvent.VK_S: // 坦克1停止向下移动
                gamePanel.homeTanks.get(0).setToD(false);
                break;
            case KeyEvent.VK_UP: // 坦克2停止向上移动
                if (gamePanel.gameType == GameType.TWO_PLAYER_MODE)
                    gamePanel.homeTanks.get(1).setToU(false);
                break;
            case KeyEvent.VK_DOWN: // 坦克2停止向下移动
                if (gamePanel.gameType == GameType.TWO_PLAYER_MODE)
                    gamePanel.homeTanks.get(1).setToD(false);
                break;
            case KeyEvent.VK_LEFT: // 坦克2停止向左移动
                if (gamePanel.gameType == GameType.TWO_PLAYER_MODE)
                    gamePanel.homeTanks.get(1).setToL(false);
                break;
            case KeyEvent.VK_RIGHT: // 坦克2停止向右移动
                if (gamePanel.gameType == GameType.TWO_PLAYER_MODE)
                    gamePanel.homeTanks.get(1).setToR(false);
                break;
        }
    }
}
